* Union and party workers

cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

* The Job register (JOBB) contains information on all annual labor income sources above 100 SEK. Use this register to find incomes from i) unions, and ii) political party organizaitons, which we find via their industry codes. 

* The final dataset has the level of observation: person-organization-year with the variables:
* individual (anonymized id code): P1129_Lopnr_PersonNr 
* type of income: type 
* sum of the income from that organization: ind_inc
* industry code: AstSniXX
* organization code: P1129_Lopnr_PeOrgNr

* The industry code changes over time and the income variable changes name, so different years need slightly different loops.

* Prior to SNI92, unions had the same industry code as employer organisations. For these years (85-92), we define incomes as coming from unions if they came from an organization later defined as such in the five first years with the 92 industry code. Using all years risks getting too many organizations because codes are re-used.


cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

* Open the yearly dataset on job-level annual incomes, sum income from the two industry codes of interest at the individual level. Save these data as a panel.


odbc load, exec("select * from JOBB1993") dsn("P1129") clear
gen fack =AstSni92== "91200" 
bysort P1129_Lopnr_PeOrgNr: egen fack_mean = mean(fack)
tab fack_mean
keep if AstSni92== "91200" & fack_mean>0.8
duplicates drop P1129_Lopnr_PeOrgNr, force
drop if P1129_Lopnr_PeOrgNr==.
save "tempfack_id.dta", replace

odbc load, exec("select * from JOBB1992") dsn("P1129") clear
joinby P1129_Lopnr_PeOrgNr using "tempfack_id.dta", unmatched(master)

	
clear
gen drop = .
save "unionpartyinc.dta", replace


foreach year in 1985 1986 1987 1988 1989 1990 1991 1992 {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
	gen ind_inc = Lon1+Lon2 
	collapse(sum)ind_inc, by(AstSni69 P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	joinby P1129_Lopnr_PeOrgNr using "tempfack_id.dta", unmatched(master)
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field
	gen type = "politisk organisation" if AstSni69=="93991"
	count if _merge==3
	replace type = "arbetstagarorg org_id" if _merge==3
	keep if type!=""
	

	gen year=`year'
	keep year P1129_Lopnr_PersonNr type ind_inc jobinc_rank AstSni69 P1129_Lopnr_PeOrgNr
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}

tab year if type =="arbetstagarorg org_id"

foreach year in 1993 1994 {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
	ren Lon1 ind_inc
		collapse(sum)ind_inc, by(AstSni92 P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	joinby P1129_Lopnr_PeOrgNr using "tempfack_id.dta", unmatched(master)
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field
	*bysort P1129_Lopnr_PersonNr AstSni92: gen ind_inc=Lon1
	keep if AstSni92== "91200" | AstSni92=="91320"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni92=="91200"
	replace type = "verksamhet inom politisk organisation" if AstSni92=="91320"
	keep if type!=""
	
	gen year=`year'
	
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni92 P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}

* Lon1 och AstAni
foreach year in 1995 1996 {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
		ren Lon1 ind_inc
		collapse(sum)ind_inc , by(AstSni P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field 
	keep if AstSni== "91200" | AstSni=="91320"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni=="91200"
	replace type = "verksamhet inom politisk organisation" if AstSni=="91320"
	keep if type!=""
	
	gen year=`year'
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}
foreach year in{
	
	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
			ren Lon1 ind_inc
		collapse(sum)ind_inc , by(AstSni P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field 
	keep if AstSni== "91200" | AstSni=="91320"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni=="91200"
	replace type = "verksamhet inom politisk organisation" if AstSni=="91320"
	keep if type!=""
	
	gen year=`year'
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
}
* LonFInk och AstSni
foreach year in  1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
	ren LonFInk ind_inc
	destring ind_inc, force replace
		collapse(sum)ind_inc, by(AstSni P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field 
	*bysort P1129_Lopnr_PersonNr AstSni: gen ind_inc=LonFInk
	keep if AstSni== "91200" | AstSni=="91320"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni=="91200"
	replace type = "verksamhet inom politisk organisation" if AstSni=="91320"
	keep if type!=""
	
	gen year=`year'
	
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}

foreach year in 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018  {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear
	ren LonFInk ind_inc
	destring ind_inc, force replace
		collapse(sum)ind_inc, by(AstSni07 P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field 

	keep if AstSni07== "94200" | AstSni07=="94920"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni07=="94200"
	replace type = "verksamhet inom politisk organisation" if AstSni07=="94920"
	keep if type!=""
	
	gen year=`year'
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni07 P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}

foreach year in 2019 {

	odbc load, exec("select * from JOBB`year'") dsn("P1129") clear	
	ren ArsLonFInk  ind_inc
	destring ind_inc, force replace
		collapse(sum)ind_inc, by(AstSni07 P1129_Lopnr_PeOrgNr P1129_Lopnr_PersonNr)
	bysort P1129_Lopnr_PersonNr: egen jobinc_rank=rank(ind_inc), field 
	keep if AstSni07== "94200" | AstSni07=="94920"
	gen type = "intressebevakning inom arbettagarorganisation" if AstSni07=="94200"
	replace type = "verksamhet inom politisk organisation" if AstSni07=="94920"
	keep if type!=""
	
	gen year=`year'
	
	keep year P1129_Lopnr_PersonNr type ind_inc AstSni07 P1129_Lopnr_PeOrgNr jobinc_rank
	
	append using "unionpartyinc.dta", force
	save "unionpartyinc.dta", replace
	
}

*Load data on sampled Union members from the AKU. Used to identify labor unions.
 cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

 odbc load, exec("select * from AKU_1987_2019") dsn("P1129") clear
 gen year=Ar
 destring , force replace
 gen early =(year <=2004 |(year==2005 &  HaMerArb!=.)) & year!=.
  gen late = (year >=2006 |(year==2005 &  HaMerArb==.)) & year!=.
  gen worker_union = (((FackForb>0 & FackForb<30) | FackForb==86) & early==1)| (((FackForb>0 & FackForb<20) | FackForb==71) & late==1)
  gen union=FackAnsl==1 if FackAnsl==1 | FackAnsl==2
  gen working= Grad==1|Grad==2|Grad==3|Grad==11|Grad==12
  gen union_wp=union if working==1
    gen worker_union_wp=worker_union if working==1
	keep LopNr year worker_union- worker_union_wp
	ren LopNr p_id
	compress
	save aku_union, replace
	keep if year!=.
	collapse (max)union worker_union, by (p_id year)
	replace worker_union=. if union==.
	fillin p_id year
	sort p_id year
	gen union_latest=union
	replace union_latest = union_latest[_n-1] if union_latest==. & p_id==p_id[_n-1]
	gen worker_union_latest=worker_union
	replace worker_union_latest = worker_union_latest[_n-1] if worker_union_latest==. & p_id==p_id[_n-1]
 ren year valar
 keep p_id valar union_latest worker_union_latest
 keep if valar==1982 | valar==1985 | valar==1988 | valar==1991 | valar==1994 | valar==1998 | valar==2002 | valar==2006| valar==2010 | valar==2014 | valar==2018 
 save unionmemb_prev, replace
	
use "unionpartyinc.dta", clear

duplicates drop year P1129_Lopnr_PersonNr P1129_Lopnr_PeOrgNr, force
gen fack = 1 if type=="arbetstagarorg org_id"
replace fack = 1 if type =="intressebevakning inom arbettagarorganisation"
keep if fack==1

ren P1129_Lopnr_PersonNr p_id
*Add data on members
joinby p_id year using aku_union, unmatched(master)
replace worker_union=. if union!=1
bysort P1129_Lopnr_PeOrgNr: egen union_ant=sum(union)
bysort P1129_Lopnr_PeOrgNr: egen worker_union_sh=mean(worker_union)

gen union_worker= worker_union_sh>=.80 if worker_union_sh!=. & union_ant>=10 & union_ant!=. 
gen union_nonworker= worker_union_sh<=.20 if worker_union_sh!=. & union_ant>=10 & union_ant!=. 
gen union_undef=union_worker!=1 & union_nonworker!=1

gen pbb =.
replace	pbb	=	46500	if 	year 	==	2019
replace	pbb	=	45500	if 	year 	==	2018
replace	pbb	=	44800	if 	year 	==	2017
replace	pbb	=	44300	if 	year 	==	2016
replace	pbb	=	44500	if 	year 	==	2015
replace	pbb	=	44400	if 	year 	==	2014
replace	pbb	=	44500	if 	year 	==	2013
replace	pbb	=	44000	if 	year 	==	2012
replace	pbb	=	42800	if 	year 	==	2011
replace	pbb	=	42400	if 	year 	==	2010
replace	pbb	=	42800	if 	year 	==	2009
replace	pbb	=	41000	if 	year 	==	2008
replace	pbb	=	40300	if 	year 	==	2007
replace	pbb	=	39700	if 	year 	==	2006
replace	pbb	=	39400	if 	year 	==	2005
replace	pbb	=	39300	if 	year 	==	2004
replace	pbb	=	38600	if 	year 	==	2003
replace	pbb	=	37900	if 	year 	==	2002
replace	pbb	=	36900	if 	year 	==	2001
replace	pbb	=	36600	if 	year 	==	2000
replace	pbb	=	36400	if 	year 	==	1999
replace	pbb	=	36400	if 	year 	==	1998
replace	pbb	=	36300	if 	year 	==	1997
replace	pbb	=	36200	if 	year 	==	1996
replace	pbb	=	35700	if 	year 	==	1995
replace	pbb	=	35200	if 	year 	==	1994
replace	pbb	=	34400	if 	year 	==	1993
replace	pbb	=	33700	if 	year 	==	1992
replace	pbb	=	32200	if 	year 	==	1991
replace	pbb	=	29700	if 	year 	==	1990
replace	pbb	=	27900	if 	year 	==	1989
replace	pbb	=	25800	if 	year 	==	1988
replace	pbb	=	24100	if 	year 	==	1987
replace	pbb	=	23300	if 	year 	==	1986
replace	pbb	=	21800	if 	year 	==	1985

gen union_worker_mainjob=ind_inc>(3.5*pbb) & jobinc_rank==1 & union_worker==1
gen union_monworker_mainjob=ind_inc>(3.5*pbb) & jobinc_rank==1 & union_nonworker==1

collapse (max)union_worker union_nonworker union_undef fack union_worker_mainjob union_monworker_mainjob , by(p_id year)

save union_year, replace


***Extract data from LISA that is later used to construct definition of workers
	cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
	
odbc load, exec("select p_id=LopNr , ssyk4=Ssyk4  , YSeG, YrkStalln, StudMed, AstSNI92, ExamAr, ForvErs,MedbGrEg4 from LISA_2001") dsn("P1129") clear
gen year=2001
save lisa_panel, replace
	forvalues year=2002(1)2010{
odbc load, exec("select  p_id=LopNr,  ssyk4=Ssyk4  , YSeG, YrkStalln, StudMed, AstSNI2002, ExamAr, ForvErs,MedbGrEg4  from LISA_`year'") dsn("P1129") clear
	gen year=`year'
	append using lisa_panel
save lisa_panel	, replace
	}
		forvalues year=2011(1)2013{
odbc load, exec("select  p_id=LopNr,  ssyk4=Ssyk4  , YSeG, YrkStalln, StudMed, AstSNI2007, ExamAr, ForvErs ,MedbGrEg4 from LISA_`year'") dsn("P1129") clear
	gen year=`year'
	append using lisa_panel
save lisa_panel	, replace
	}

		forvalues year=2014(1)2016{
odbc load, exec("select  p_id=LopNr, ssyk4_2012=Ssyk4_2012  , ESeG_J16, YrkStalln, StudMed, AstSNI2007, ExamAr, ForvErs,MedbGrEg4  from LISA_`year'") dsn("P1129") clear
	gen year=`year'
	append using lisa_panel
save lisa_panel	, replace
	}

	forvalues year=2017(1)2019{
odbc load, exec("select  p_id=LopNr, ssyk4_2012=Ssyk4_2012_J16  , ESeG_J16, YrkStalln, StudMed, AstSNI2007, ExamAr, ForvErs,MedbGrEg4  from LISA_`year'") dsn("P1129") clear
	gen year=`year'
	append using lisa_panel
save lisa_panel	, replace
	}
	compress
	destring, force replace
	
	joinby ssyk4_2012 using ssyk4_2012_to_ssyk96.dta , unmatched(master)
	drop _merge
	replace ssyk4= ssyk96kod if year>=2014
	gen ssyk96 =int(ssyk4/10)
	joinby ssyk96 using "ssyk96_esec_egp", unmatched(master)
	drop _merge
	joinby AstSNI2007 using "sni2007_to_sni2002" , unmatched(master)
	replace AstSNI2002 =AstSNI2002_con if AstSNI2002==. & AstSNI2002_con!=. 
	drop _merge  AstSNI2002_con
	joinby AstSNI92 using "sni92_to_sni2002" , unmatched(master)
	replace AstSNI2002 =AstSNI2002_con if AstSNI2002==. & AstSNI2002_con!=.
	drop _merge  AstSNI2002_con
	replace AstSNI2002=0 if AstSNI2007==0 |AstSNI92==0
	// 2002-
	compress
recode AstSNI2002 (150/379=1)(.=.)(else=0)					      , gen(manufacturing)
recode AstSNI2002 (500/749 900/909 920/939=1)(.=.)(else=0)		  , gen(priv_service)
recode AstSNI2002 (750/759 800/809 850/859=1)(.=.)(else=0)		  , gen(pub_service)
gen elseind = 0 if AstSNI2002!=.
replace elseind = 1 if manufacturing==0 & priv_service==0 & pub_service==0
gen industry = manufacturing*1 + priv_service*2 + pub_service*3+elseind*4
drop manufacturing- elseind 
ren ssyk96 ssyk3
mmerge ssyk3 industry using "isco_sei_key_2", type(n:1) unmatched(master) missing(nomatch) umatch(isco3 industry)
ren modsei sei
gen citizen= MedbGrEg4==0
drop _merge pct Ntot industry MedbGrEg4
save lisa_panel	, replace

*Extract data from FOB that is used to define the working class

cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
odbc load, exec("select p_id=LopNr, kon=Kon, age=Alder,  Yrke, YrkeStall, syss=Syss,   mun=Kommun, SEI, Medb  from FoB_60Ind") dsn("P1129") clear	
gen year=1960
drop if age<18
save fob_panel, replace
foreach year in  70{
odbc load, exec("select p_id=LopNr, kon=Kon, age=Alder,  Yrke, YrkeStall, syss=Syss,   mun=Kommun, SEI, Medb  from FoB_`year'Ind") dsn("P1129") clear	
gen year=19`year'
append using fob_panel
drop if age<18
save fob_panel, replace
}
foreach year in   75{
odbc load, exec("select p_id=LopNr, kon=Kon, age=Alder,  Yrke, YrkeStall, syss=Syss,   mun=Kommun , Medb from FoB_`year'Ind") dsn("P1129") clear	
gen year=19`year'
drop if age<18
append using fob_panel

save fob_panel, replace
}
foreach year in  80 {
odbc load, exec("select p_id=LopNr, kon=Kon, age=Alder,  Yrke, syss=Syss,   mun=Kommun, SEI, Medb  from FoB_`year'Ind") dsn("P1129") clear	
gen year=19`year'
drop if age<18
append using fob_panel
save fob_panel, replace
}
foreach year in   85 90{
odbc load, exec("select p_id=LopNr, kon=Kon, age=Alder,  Yrke=Yrke80, Yrke85, YrkeStall, syss=Syss,   mun=Kommun, SEI, Medb  from FoB_`year'Ind") dsn("P1129") clear	
gen year=19`year'
drop if age<18
append using fob_panel
save fob_panel, replace
}

destring YrkeStall, force replace
destring Yrke, force replace
ren Yrke yrke
recode YrkeStall(0 1 2 = 0) (3 4 = 1) (9=.) if year==1970, gen(entrepreneur70)
recode  YrkeStall (0 1  = 0) (2 3 = 1) (9=.) if year==1975, gen(entrepreneur75)
gen entrepreneur60=0    if yrke !=0 & yrke !=. & year==1960 
replace entrepreneur60=1            if ((YrkeStall==8|YrkeStall==7) & yrke != 0 & yrke !=.) & year==1960
gen entrepreneur=entrepreneur60
replace entrepreneur=entrepreneur70 if year==1970
replace entrepreneur=entrepreneur75 if year==1975
	  
gen politician=yrke==101
save fob_panel, replace

odbc load, exec("select p_id=Lopnr, Utb60=Utbild from FoB_60Ind_SUN_SNI") dsn("P1129") clear
joinby p_id using fob_panel, unmatched(using)
drop _merge
save fob_panel, replace

odbc load, exec("select p_id=Lopnr, Utb70=UtbNiva  from FoB_70Ind_SUN_SNI") dsn("P1129") clear
joinby p_id using fob_panel, unmatched(using)
drop _merge
duplicates drop p_id year, force
save fob_panel, replace
destring Utb60 Utb70, force replace
gen utb = mod(Utb60,10) if year ==1960
tab utb  // 0 = basic, 3 = studentex, 4, lägre TE,  5 = fil kand, civ ing, 6 fil lic/dr
recode utb (0=1) (3=4) (4=5) (5=6) (6=7)        // för att matcha fob70!
replace utb = Utb70 if Utb70>utb  & Utb70<. & age>=25  & year==1960  // reasonable imputation
replace utb = Utb70 if utb==.              & age>=25  & year==1960  // reasonable imputation
replace utb = Utb70 if  year>1960 & year<1985
drop  entrepreneur60 entrepreneur70 entrepreneur75 Utb70 Utb60 
destring kon, force replace
gen sei_gen =.
replace sei_gen = 11 if kon==1 & /*
	*/ inlist(yrke,43,204,333,338,411,413,412,414,415,416,431,441,503,504,611,632,633,		/*
      */  634,635,636,639,661,671,678,701,718,719,722,729,731,732,733,736,737,738, 			/*
      */  739,757,759,769,773,774,778,779,795,797,799,806,812,813,818,819,821, 			/*
      */  823,824,825,827,828,831,834,836,838,839,841,851,852,853,854,856,857, 			/* 
      */  858,861,872,873,875,876,881,882,883,888,901,904,908,913,914,915,919, 			/*
      */  932,943,944,948) 	
replace sei_gen = 21 if kon==1 &/*
	*/ inlist(yrke,42,83,87,419,501,502,631,711,716,712,713,714,715,721,726,735, 			/*
	*/  741,742,743,745,750,758,751,752,758,753,756,754, 							/*
      */  755,761,764,768,766,767,771,772,781,782,791,798,792,793,794,  				/*	
      */  801,808,811,814,822,826,855,871,874,912,921,933,941)
replace sei_gen = 36 if kon==1 &/*
	*/ inlist(yrke,8,14,45,56,59,201,290,291,293,294,295,297,301,302,321,331,334,332,402,	/* 
	*/ 403,409,421,641,642,643,644,651,653,654,655,662,903,916,931,942,946,947) 		
replace sei_gen = 46 if kon==1 &/*
	*/ inlist(yrke,1,2,3,4,5,6,7,9,23,24,40,41,47,48,53,54,55,57,58,68,69,81,82,85,          	/*
	*/  86,92,118,203,208,292,296,298,299,311,312,318,319,404,432,601,602,603,621,      	/*
	*/  652,744,765,902,911,917,945,949,981)								
replace sei_gen = 56 if kon==1 &/*
	*/ inlist(yrke,11,12,13,21,22,31,32,46,50,51,52,61,71,72,73,74,78,84,88,91,93,94,				/*
	*/  95,96,97,98,99,101,111,313)
replace sei_gen = 11 if kon==1 &/*
	*/ inlist(yrke,49,509,669,699,749,799,809,829,859,879,889,899,909,939,949)
replace sei_gen = 36 if kon==1 &/*
	*/ inlist(yrke,19,69,79,209,339,649,659)
replace sei_gen = 46 if kon==1 &/*
	*/ inlist(yrke,29,59,89,119,609)	
replace sei_gen = 56 if kon==1 &/*
	*/ inlist(yrke,39)	
/
replace sei_gen= 89 if kon==1 &/*
	*/ inlist(yrke,401)		

replace sei_gen = 89 if entrepreneur==1 & yrke>400 & yrke<500 & kon==1
replace sei_gen = 11 if entrepreneur==0 & yrke>400 & yrke<500 & kon==1
replace sei_gen = 60  if entrepreneur==1 & utb==6 & yrke>1 & yrke <7
recode yrke (11 13 21 22 23 24 31 32 39 46 51 52 57 71 72 73 74 78 84 91 94 95 97 =1) (else=0), gen(prof)
replace sei_gen=60 if entrepreneur==1 & prof==1 & kon==1
replace sei_gen=79 if entrepreneur==1 & sei_gen!=60 & sei_gen!=89 & kon==1
replace sei_gen = 11 if kon==2 & /*
	*/ inlist(yrke,43,204,333,338,411,413,412,414,415,416,431,441,503,504,611,632,633,		/*
		*/ 634,635,636,639,661,671,678,701,718,719,722,729,731,732,733,736,737,738,		/*
		*/ 739,757,759,769,773,774,778,779,795,797,799,806,812,813,818,819,821,			/*	
		*/ 823,824,825,827,828,831,834,836,838,839,841,851,852,853,854,856,857,			/*
		*/ 858,861,872,873,875,876,881,882,883,888,901,904,908,913,914,915,919,			/*
		*/ 932,943,944,948)
replace sei_gen = 21 if kon==2 & /*
	*/ inlist(yrke,42,83,87,419,501,502,631,711,716,712,713,714,715,721,726,735,			/*
		*/ 741,742,743,745,750,758,751,752,758,753,756,754,755,761,					/*
		*/ 764,768,766,767,771,772,781,782,791,798,792,793,794,					/*
		*/ 801,808,811,814,822,826,855,871,874,912,921,933,941)	
replace sei_gen = 36 if kon==2 & /*
	*/ inlist(yrke,8,14,45,56,59,201,290,291,293,294,295,							/*
		*/ 297,301,302,321,331,334,332,402,									/*
		*/ 403,409,421,641,642,643,644,651,653,654,655,662,903,916,931,942,946,			/*	
		*/ 947)
replace sei_gen = 46 if kon==2 & 												/*
	*/ inlist(yrke,1,2,3,4,5,6,7,9,23,24,40,41,47,48,53,54,55,57,58,68,69,81,82,85,		/*
		*/ 86,92,118,203,208,292,296,298,299,311,312,							/*
		*/ 318,319,404,432,601,602,603,621,									/*
		*/ 652,744,902,911,917,945,949,981)	
replace sei_gen = 56 if kon==2 & /*
	*/ inlist(yrke,11,12,13,21,22,31,32,46,50,51,52,61,71,72,73,74,78,84,88,91,93,94,		/*
		*/ 95,96,97,98,99,101,111,313)	
replace sei_gen = 11 if kon==2 & /*
	*/ inlist(yrke,418,501,631,651,711,716,									/*
		*/ 712,713,721,726,735,738,750,758,751,752,758,753,756,755,					/*
		*/ 764,766,767,772,781,782,811,822,826,918,921,942,947)
replace sei_gen = 21 if kon==2 & /*
	*/ inlist(yrke,797)	

replace sei_gen = 36 if kon==2 & /*
	*/ inlist(yrke,9,44,81,296,298,765,808,917,945)		
replace sei_gen = 46 if kon==2 & /*
	*/ inlist(yrke,11,12,59,61,96,313)										
replace sei_gen = 56 if kon==2 & /*
	*/ inlist(yrke,1) 
replace sei_gen = 11 if kon==2 & /*
	*/ inlist(yrke,49,509,669,699,749,799,809,829,859,879,889,899,909,				/*
		*/ 939,949)	
replace sei_gen = 36 if kon==2 & /*
	*/ inlist(yrke,19,69,79,209,339,649,659)	
replace sei_gen = 46 if kon==2 & /*
	*/ inlist(yrke,29,59,89,119,609)	
replace sei_gen = 56 if kon==2 & /*
	*/ inlist(yrke,39)
replace sei_gen = 89 if kon==2 & /*
	*/ inlist(yrke,401)
replace sei_gen = 89 if entrepreneur==1 & yrke>400 & yrke<500 & kon==2
replace sei_gen = 11 if entrepreneur==0 & yrke>400 & yrke<500 & kon==2
replace sei_gen = 60  if entrepreneur==1 & utb==6 & yrke>1 & yrke <7
destring syss SEI, force replace
gen sei = SEI if year>=1980 & SEI<90
replace sei =11 if sei==12
replace sei =21 if sei==22
replace sei =36 if sei==33
replace sei =56 if sei==57
replace sei=sei_gen if year<=1975
replace sei= 95 if age>=65
replace sei=97 if year==1960 & SEI==11
replace sei=97 if year==1970 & syss==6
replace sei=97 if year==1975 & syss==6
replace sei=97 if year==1980 & syss==7
save fob_panel, replace
odbc load, exec("select p_id=Lopnr, STUD from IoT_1985") dsn("P1129") clear
gen year=1985
joinby p_id year using fob_panel, unmatched(using)
replace sei=97 if year==1985 & STUD>0 & STUD!=. & sei==.
drop _merge STUD
save fob_panel, replace
odbc load, exec("select p_id=Lopnr, StudMed from LISA_1990") dsn("P1129") clear
gen year=1990
joinby p_id year using fob_panel, unmatched(using)
replace sei=97 if year==1990 & StudMed>0 & StudMed!=. & sei==.
drop _merge StudMed
duplicates drop p_id year, force
ren Yrke85 nyk85
gen nyk80 = yrke if year==1980
destring nyk85 nyk80, force replace
joinby nyk85 using nyk_85_to_isco.dta, unmatched(master)
save fob_panel, replace
keep sei yrke p_id year  mun isco Medb
gen citizen = 0 if year>=1970
replace citizen=1 if Medb=="  1" & (year==1985 |year==1990)
destring Medb, force replace
replace citizen=1 if (Medb<200|Medb==.) & year==1970
replace citizen=1 if Medb<200 & (year==1975 |year==1980)
drop Medb
save sei_panel, replace

*compile data into main dataset with definition of workers and contruct the different alternative defintions
cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_Sams_1994") dsn("P1129") clear
gen year=1994
save temp, replace
odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_ValDist_1998") dsn("P1129") clear
gen year=1998
append using tempp
save temp, replace
	
forvalues year=2001/2019{
odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_ValDist_`year'") dsn("P1129") clear
gen year=`year'
append using temp
save temp, replace
}

use lisa_panel, clear
keep sei ssyk4 year p_id StudMed isco88 citizen
ren isco88 isco88_lisa
replace sei=97 if  StudMed>0 & StudMed!=. & sei==.
joinby p_id year using  "temp.dta" , unmatched(master)
drop _merge
joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\by_sex.dta" , unmatched(master)
destring FodelseAr, force replace

gen age=year-FodelseAr
drop if age<18
replace sei= 95 if age>=65
keep sei ssyk4 year p_id   StudMed isco88 citizen  woman mun


append using sei_panel
replace isco88= isco88_lisa if isco88_lisa!=.
replace isco88=int(isco88/10) if year <=1990

drop woman isco88_lisa
compress
joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\byi_sex.dta" , unmatched(master)
destring FodelseAr, force replace

gen age=year-FodelseAr
drop if age<18
drop Fodelse  _merge
duplicates drop p_id year, force

joinby p_id year using "union_emp.dta", unmatched (master)
drop _merge
gen politician=yrke==101 | ssyk4==1110 
drop if age<18
gen sei_imp=sei if  politician!=1 & sei!=.
sort p_id year
forvalues n=1/15{ 
replace sei_imp=sei_imp[_n-`n'] if sei_imp==.  & politician[_n-`n']!=1 & sei[_n-`n']!=. & p_id==p_id[_n-`n']
}
gen sei_lim_imp=sei if  politician!=1 & sei!=. & sei!=95
sort p_id year
forvalues n=1/15{ 
replace sei_lim_imp=sei_lim_imp[_n-`n'] if sei_lim_imp==.  & politician[_n-`n']!=1 & sei[_n-`n']!=. & sei[_n-`n']<95 & p_id==p_id[_n-`n']
}

gen worker= sei_imp<30
gen worker_lim=sei_lim_imp<30 if sei_lim_imp!=. &  sei_lim_imp!=95
drop  yrke
compress
joinby p_id year using "union_year.dta", unmatched (both)
drop _merge union_undef

foreach var in union_worker_mainjob fack union_worker  union_nonworker  {
replace `var'=0 if `var'==.	
}
ren fack union 
compress

sort p_id year
foreach var in union_worker_mainjob union_worker worker union {
	gen `var'_prev=`var'
forvalues n=1/40{ 
replace `var'_prev=1 if `var'[_n-`n']==1  & p_id==p_id[_n-`n']
}
	
}

gen dist_37=abs(37.1-age) if year!= 1994 & year!=1998
bysort p_id : egen mindist_37=min(dist_37)
gen base=worker_lim  if dist_37 ==mindist_37 & age>=35 & age<=40 
bysort p_id :egen worker_37=mean(base) 
drop dist_37-base
 *worker_nulim
drop int_org
gen base =year if politician==1
bysort p_id :egen fy_pol=min(base)
drop base
gen base =worker if (year<fy_pol | fy_pol==.) & sei!=95 & age<65 & year!= 1994 & year!=1998
bysort p_id : egen worker_mode=mode(base)
drop base fy_pol
compress

foreach var in isco88 ssyk4  {
	gen `var'_seiy = `var' if  politician!=1 & sei!=.  & sei<95
	sort p_id year
	forvalues n=1/15{ 
		replace `var'_seiy=`var'[_n-`n'] if `var'_seiy==.  & politician[_n-`n']!=1 & sei[_n-`n']!=.  & sei[_n-`n']<95 & p_id==p_id[_n-`n']
	}
}
gen worker_isco= isco88_seiy>=400 if isco88_seiy!=.
gen worker_cit= worker_lim if citizen==1
compress 
save  sei_panel, replace
preserve 
joinby p_id using "pol_id.dta", unmatched(none)
save sei_panel_pol, replace
restore
***Construct data set with yearly averages later used to in main analysis
preserve
keep worker* union*  year
collapse (mean)worker* union*, by(year)
foreach var of varlist  worker -union_worker_prev{
	ren `var' `var'_pop_y
}
save worker_pop_y, replace
restore
preserve
keep  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit year age
collapse (mean)  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit, by(year age)
foreach var of varlist  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit{
	ren `var' `var'_pop_ya
}
save worker_pop_ya, replace
restore
preserve
keep union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit   year mun
collapse (mean) union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit , by(year mun)
foreach var of varlist  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit{
	ren `var' `var'_pop_ym,
}
ren mun llkk
do "mun code changes"
ren llkk mun
save worker_pop_ym, replace
restore
preserve
keep  worker*   woman  year mun
collapse (mean) worker_lim , by(year mun woman)
ren  worker_lim  worker_lim_pop_ygm
ren mun llkk
do "mun code changes"
ren llkk mun
save worker_pop_ygm, replace
restore
preserve
keep  worker*  woman  year

collapse (mean) worker_lim , by(year woman)
ren  worker_lim  worker_lim_pop_yg

save worker_pop_yg, replace
restore 

preserve
keep  sei_lim_imp year mun 
keep if year==2006|year==2010|year==2014 |year==2018
gen group= "w" if sei_lim_imp<=21 
replace group= "nw" if sei_lim_imp>= 30 & sei_lim_imp<=70 
replace group= "e" if sei_lim_imp>= 70 & sei_lim_imp<=89 
replace group= "s" if sei_lim_imp==97

gen count =1
collapse (sum) count, by(year mun group)
ren mun llkk
do "mun code changes"
ren llkk mun
bysort mun year:egen sum=sum(count)
gen sh_=count/sum
joinby year group using psu_tomerge, unmatched(none)
drop count sum
reshape long votes_@, i(year mun group sh_) j(parti_initial) string
gen shp= votes_*sh_
bysort year mun parti: egen sum =sum(shp)
replace shp= shp/sum  
keep if group=="w"
ren shp wsh_pvoters
keep  wsh_pvoters parti mun year

save worker_party_ym, replace
restore

preserve
keep  sei_lim_imp year 
keep if year==2006|year==2010|year==2014 |year==2018
gen group= "w" if sei_lim_imp<=21 
replace group= "nw" if sei_lim_imp>= 30 & sei_lim_imp<=70 
replace group= "e" if sei_lim_imp>= 70 & sei_lim_imp<=89 
replace group= "s" if sei_lim_imp==97

gen count =1
collapse (sum) count, by(year  group)

bysort year:egen sum=sum(count)
gen sh_=count/sum
joinby year group using psu_tomerge, unmatched(none)
drop count sum
reshape long votes_@, i(year group sh_) j(parti_initial) string
gen shp= votes_*sh_
bysort year parti: egen sum =sum(shp)
replace shp= shp/sum  
keep if group=="w"
ren shp wsh_y_pvoters
keep  wsh_y_pvoters parti  year

save worker_party_y, replace
restore


preserve
keep  sei_lim_imp year mun 
gen group= "w" if sei_lim_imp<=21 
replace group= "nw" if sei_lim_imp>= 30 & sei_lim_imp<=90 
replace group= "s" if sei_lim_imp==97

gen count =1
collapse (sum) count, by(year mun group)
ren mun llkk
do "mun code changes"
ren llkk mun
bysort mun year:egen sum=sum(count)
gen sh_=count/sum
joinby year group using valu_tomerge, unmatched(none)
drop count sum year
reshape long votes_@, i(valar mun group sh_) j(parti_initial) string
gen shp= votes_*sh_
bysort valar mun parti: egen sum =sum(shp)
replace shp= shp/sum  
keep if group=="w"
ren shp wsh_valu_pvoters
keep wsh_valu_pvoters parti mun valar

save worker_valu_party_ym, replace
restore
preserve
keep  sei_lim_imp year mun 
gen group= "w" if sei_lim_imp<=21 
replace group= "nw" if sei_lim_imp>= 30 & sei_lim_imp<=90 
replace group= "s" if sei_lim_imp==97

gen count =1
collapse (sum) count, by(year  group)

bysort  year:egen sum=sum(count)
gen sh_=count/sum
joinby year group using valu_tomerge, unmatched(none)
drop count sum
reshape long votes_@, i(valar group sh_) j(parti_initial) string
gen shp= votes_*sh_
bysort valar parti: egen sum =sum(shp)
replace shp= shp/sum  
keep if group=="w"
ren shp wsh_y_valu_pvoters
keep wsh_y_valu_pvoters parti   valar

save worker_valu_party_y, replace
restore

preserve
keep  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit  year age mun

collapse (mean)  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit  , by(year age mun)

foreach var of varlist  union_worker_prev worker worker_lim union_worker union_nonworker  worker_prev union_prev worker_37 worker_mode worker_isco worker_cit {
	ren `var' `var'_pop_yam
}
ren mun llkk
do "mun code changes"
ren llkk mun
save worker_pop_yam, replace
restore


***Define Parental Working Class Status
cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
	
use sei_panel, clear
keep sei_imp p_id year age worker
ren p_id par_id 
joinby par_id using pol_parent , unmatched(none)

gen dist_37=abs(37.1-age)
bysort par_id : egen mindist_37=min(dist_37)
gen sei_pa37_=sei_imp if dist_37 ==mindist_37 & age>=35 & age<=40
gen worker_pa37_=worker  if dist_37 ==mindist_37 & age>=35 & age<=40 

joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta" , unmatched(master)
destring FodelseAr, force replace
gen age_pol=year-FodelseAr
gen dist_17=abs(17.1-age_pol)
bysort par_id : egen mindist_17=min(dist_17)
gen sei_pol17_=sei_imp if dist_17 ==mindist_17 & age_pol>=15 & age_pol<=20
gen worker_pol17_=worker if dist_17 ==mindist_17 & age_pol>=15 & age_pol<=20
collapse (min) sei_pa37_ worker_pa37_ worker_pol17_ sei_pol17_, by(p_id parent)
reshape wide sei_pa37_@ sei_pol17_@ worker_pa37_@ worker_pol17_@ , i(p_id) j(parent) string
save sei_pol_par, replace

*Define flexibility at occupation level using the work environment survey.
cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
use "\\micro.intra\Projekt\P1129$\P1129_Gem\Work environment\arbmiljo.dta" , clear

gen non_daytime=arbskift !=1 if arbskift!=.
gen no_task_flex=besttide
gen notime_friends_family=6-saknatid
gen overtime= 6-overtide
gen no_flex= flextid==3 if flextid!=.
drop if ssyk4==.
collapse (mean)non_daytime overtime no_flex  no_task_flex notime_friends_family, by(ssyk4)
gen ssyk4_seiy=ssyk4
save occ_timeflex, replace
	
****Set up of income panel
clear 
	cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

forvalues year=1968/2019{
if `year'<=1977{
odbc load, exec("select p_id=LopNr, p_ink=SNIN from IoT_`year'") dsn("P1129") clear		
}

if `year'>=1978 & `year'<=1989{
odbc load, exec("select p_id=LopNr,    p_ink=CARB, mun=KOMB  from IoT_`year'") dsn("P1129") clear	
}
if `year'==1990{
	odbc load, exec("select p_id=LopNr , f_ink=FInk90 , l_ink=LoneInk from LISA_`year'") dsn("P1129") clear
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
}

if `year'>=1991 &`year'<=2003{
	odbc load, exec("select p_id=LopNr,  f_ink=FInk , l_ink=LoneInk from LISA_`year'") dsn("P1129") clear
gen p_ink =l_ink +f_ink 
replace p_ink =l_ink if f_ink<0
}

if `year'>=2004 & `year'<=2019{
	odbc load, exec("select p_id=LopNr,  f_ink=InkFNetto , l_ink=LoneInk   from LISA_`year'") dsn("P1129") clear
gen p_ink =l_ink +f_ink 
replace p_ink =l_ink if f_ink<0
}
if `year'<=1970{
save temp, replace
odbc load, exec("select p_id=LopNr  from FoB_70Ind") dsn("P1129") clear
joinby p_id using temp, unmatched(both)
replace p_ink=0 if p_ink==.
drop _merge
} 

if `year'>=1990 &`year'<=1996{ 
	save temp, replace
	odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_SAMS_`year'") dsn("P1129") clear
	joinby p_id using temp, unmatched(both)
	drop _merge
}
	if `year'>=1997 &`year'<=2019{
	save temp, replace
	odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_ValDist_`year'") dsn("P1129") clear
	joinby p_id using temp, unmatched(both)
		drop _merge
}
	if `year'>=1968 &`year'<=1969{
save temp, replace
	odbc load, exec("select p_id=LopNr,  mun=Kommun from FoB_65Ind") dsn("P1129") clear
	joinby p_id using temp, unmatched(both)
		drop _merge
}
	if `year'>=1970 &`year'<=1974{
	save temp, replace
	odbc load, exec("select p_id=LopNr,  mun=Kommun from FoB_70Ind") dsn("P1129") clear
	joinby p_id using temp, unmatched(both)
		drop _merge
}
	if `year'>=1975 &`year'<=1977{
			save temp, replace
	odbc load, exec("select p_id=LopNr,  mun=Kommun from FoB_75Ind") dsn("P1129") clear
	joinby p_id using temp, unmatched(both)
		drop _merge
}
duplicates drop p_id, force
joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta" , unmatched(master)
destring FodelseAr, force replace
gen year=`year'
gen age=year-FodelseAr
gen ink_rest = p_ink if age<65 & age >=18
fasterxtile pct=ink_rest,  nq(100) 
fasterxtile pct_m=ink_rest , by(mun) nq(100) 
fasterxtile pct_ma=p_ink , by(mun age) nq(100) 
fasterxtile pct_mag=p_ink,  by(mun age Kon) nq(100) 
fasterxtile pct_age=p_ink, by(age) nq(100)
fasterxtile pct_genage=p_ink, by( age Kon) nq(100)


keep p_ink age p_id year pct*
joinby p_id using "pol_id.dta", unmatched(none)
if `year'>=1969{
append using  pol_incpanel
}
save pol_incpanel, replace
}

drop if age<18
joinby p_id using pol_first, unmatched(master)

drop _merge
sort p_id year
foreach var of varlist pct- pct_genage {
	replace `var'=. if (year> pol_first & pol_first!=.) | age >=60
	replace `var'=`var'[_n-1] if `var'==. & p_id==p_id[_n-1]
}

save pol_incpanel, replace

*Set up education panel
clear 
	cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

odbc load, exec("select * from FoB_70Ind_SUN_SNI") dsn("P1129") clear		

ren Lopnr p_id
destring UtbNiva, force replace
ren UtbNiva edu_1970
keep p_id edu
gen samp_1970=1

save edu_panel, replace
odbc load, exec("select p_id=LopNr ,  Sun2000Niva,  ExamAr from LISA_1990") dsn("P1129") clear	
tab Exam
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
destring Sun2000Niva, replace force
replace  Sun2000Niva=. if Sun2000Niva>=999

gen ed_lev=.
replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999	
joinby p_id using  edu_panel, unmatched(both)
drop _merge
destring ExamAr, force replace

joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta" , unmatched(master)
destring FodelseAr, force replace

foreach year in 1970 1973 1975 1976 1979 1980 1982 1985 1988{
gen ed_level`year'= ed_lev if ExamAr<=`year' & ExamAr!=.	
replace ed_level`year'= ed_lev if `year'- FodelseAr>25 & ed_level`year'	
replace ed_level`year'= edu_1970  if  FodelseAr<1946 & ed_level`year'==.
}
gen ed_level1990= ed_lev
keep p_id ed_level*
duplicates drop p_id, force
reshape long ed_level@, i(p_id) j(year)

drop if ed_level==.
compress
save edu_panel, replace

foreach year in 1991 1994 1998{
odbc load, exec("select p_id=LopNr ,  Sun2000Niva from LISA_1990") dsn("P1129") clear	
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999

	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen year=`year'
	keep year p_id  ed_level
	append using  edu_panel
	save edu_panel, replace
}
forvalues year=2001/2017{
odbc load, exec("select p_id=LopNr ,  Sun2000Niva from LISA_`year'") dsn("P1129") clear	
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999

	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen year=`year'
	keep year p_id  ed_level
	append using  edu_panel
	save edu_panel, replace	
}
odbc load, exec("select p_id=LopNr ,  Sun2000Niva from LISA_2018") dsn("P1129") clear	
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999

	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen year=2018
	keep year p_id  ed_level
	append using  edu_panel
	save edu_panel, replace	
	
	preserve 
	keep if inlist(year, 1973,1976, 1979, 1982, 1985, 1988, 1991, 1994, 1998,2002, 2006, 2010, 2014, 2018   )
	joinby p_id using  "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta" , unmatched(master)
	destring FodelseAr, replace
	gen edu1=ed_lev<=2 if ed_lev!=.
	gen edu2=ed_lev>=3 & ed_lev<=4 if ed_lev!=.
	gen edu3=ed_lev>=5 if ed_lev!=.
	gen edu1_a25_65=ed_lev<=2 if ed_lev!=. & (year-FodelseAr)>=25 & (year-FodelseAr)<65
	gen edu2_a25_65=ed_lev>=3 & ed_lev<=4 if ed_lev!=. & (year-FodelseAr)>=25 & (year-FodelseAr)<65
	gen edu3_a25_65=ed_lev>=5 if ed_lev!=. & (year-FodelseAr)>=25 & (year-FodelseAr)<65
	
	collapse (mean)edu*, by(year)
	save edu_yearmean, replace
	restore

preserve 
joinby p_id using "pol_id.dta", unmatched(none)
save edu_panel_pol, replace	
restore

*Define education and income in the same years that we measure class
	cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"
	
use sei_panel_pol, clear

joinby p_id year using  pol_incpanel, unmatched(master)

drop _merge
joinby p_id year using edu_panel_pol, unmatched(master)

foreach var in  ed_level pct pct_m  pct_ma pct_age pct_genage{
	gen `var'_seiy = `var' if  politician!=1 & sei!=.

	sort p_id year
	forvalues n=1/15{ 
		replace `var'_seiy=`var'[_n-`n'] if `var'_seiy==.  & politician[_n-`n']!=1 & sei[_n-`n']!=. & p_id==p_id[_n-`n']
	}
}

keep p_id year  ed_level_seiy- pct_genage_seiy
duplicates drop p_id year, force
save edu_inc_seiyears, replace

cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Moas folder\data"
clear

*** utb ***
use "edu_panel70_90.dta"
keep if year ==1970
gen ter_ed=ed_level>=5 if ed_level!=.
keep p_id ter_ed 
save temp, replace 
*** ink, sni, kommun ***
odbc load, exec("select p_id=LopNr, p_ink=SNIN from IoT_1970") dsn("P1129") clear		
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace
*** sni från fob ***
odbc load, exec("select p_id=Lopnr, sni69=SNI4 from FoB_70Ind_SUN_SNI") dsn("P1129") clear		
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace
** kommun från fob ***
odbc load, exec("select p_id=Lopnr, mun=Kommun from FoB_70Ind") dsn("P1129") clear		
joinby p_id using temp, unmatched(both)
drop _merge
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta", unmatched(master)
ren Kon kon
ren FodelseAr yob
destring yob, replace 
gen age = 1970-yob
keep p_id ter_ed p_ink mun sni69 kon age 
destring *, replace 	

** gör sni till 2-siffrig ***
gen sni2_69 = int(sni69/100)
*** merga med crosswalken ***
joinby sni2_69 using sni69_92, unmatched(both)

gen ast1 = int(sni92/1000) 
sum ast1
bysort p_id: egen ast= mode(ast1), minmode
keep mun ast p_id age kon ter_ed p_ink sni69 
duplicates drop

****gör om så det stämmer för SNI69 och för tvåsiffrig kod
gen     anst_15kat=.
replace anst_15kat= 1 if (ast>=1 & ast<5) 
replace anst_15kat= 2 if ast>=5 & ast<10
replace anst_15kat= 3 if ast>=10 & ast<15
replace anst_15kat= 4 if ast>=15 & ast<40
replace anst_15kat= 5 if ast>=40 & ast<45
replace anst_15kat= 6 if ast>=45 & ast<50
replace anst_15kat= 7 if ast>=50 & ast<55
replace anst_15kat= 8 if ast>=55 & ast<60
replace anst_15kat= 9 if ast>=60 & ast<65
replace anst_15kat= 10 if ast>= 65 & ast<70
replace anst_15kat= 11 if ast>= 70 & ast<75
replace anst_15kat= 12 if ast>=75 & ast<80
replace anst_15kat= 13 if ast>=80 & ast<85
replace anst_15kat= 14 if ast>=85 & ast<90
replace anst_15kat= 15 if ast>=90 & ast<96
replace anst_15kat= 12 if ast==99
gen alder_5cat=int(age/5) 

egen age_sex_ed_ast=group(kon alder_5cat anst_15kat ter_ed)
gen ln_inc = ln(p_ink)
reghdfe  ln_inc, abs(age_sex_ed_ast mun) res(inc_res)
egen std_inc_res=std(inc_res)
duplicates drop
gen year = 1970
keep if std_inc_res!=.
keep p_id sni69 year std_inc_res
compress 
save ink_residuals.dta, replace 


** 1971-1978 ***
forvalues  year =1971/1978 {
*** utb ***
use "edu_panel70_90.dta"
keep if year ==`year'
gen ter_ed=ed_level>=5 if ed_level!=.
keep p_id ter_ed 
save temp, replace 
*** ink, sni, kommun ***
if `year' <=1977 {
odbc load, exec("select p_id=LopNr, p_ink=SNIN from IoT_`year'") dsn("P1129") clear		
}
if `year' >=1978 {
odbc load, exec("select p_id=LopNr, p_ink=CARB from IoT_`year'") dsn("P1129") clear			
}
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace
*** sni från fob ***
if `year' <= 1973 {
odbc load, exec("select p_id=Lopnr, sni69=SNI4 from FoB_70Ind_SUN_SNI") dsn("P1129") clear		
}
if `year' >= 1974 {
odbc load, exec("select p_id=Lopnr, sni69=SNI4 from FoB_75Ind_SUN_SNI") dsn("P1129") clear		
}
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace
** kommun från fob ***
if `year' <= 1973 {
odbc load, exec("select p_id=Lopnr, mun=Kommun from FoB_70Ind") dsn("P1129") clear		
}
if `year' >= 1974 {
odbc load, exec("select p_id=Lopnr, mun=Kommun from FoB_75Ind") dsn("P1129") clear		
}
joinby p_id using temp, unmatched(both)
drop _merge
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta", unmatched(master)
ren Kon kon
ren FodelseAr yob
destring yob, replace 
gen age = `year'-yob
keep p_id ter_ed p_ink mun sni69 kon age 
destring *, replace 	

** gör sni till 2-siffrig ***
gen sni2_69 = int(sni69/100)
*** merga med crosswalken ***
joinby sni2_69 using sni69_92, unmatched(both)

gen ast1 = int(sni92/1000) 
sum ast1
bysort p_id: egen ast= mode(ast1), minmode
keep mun ast p_id age kon ter_ed p_ink sni69 
duplicates drop

****gör om så det stämmer för SNI69 och för tvåsiffrig kod
gen     anst_15kat=.
replace anst_15kat= 1 if (ast>=1 & ast<5) 
replace anst_15kat= 2 if ast>=5 & ast<10
replace anst_15kat= 3 if ast>=10 & ast<15
replace anst_15kat= 4 if ast>=15 & ast<40
replace anst_15kat= 5 if ast>=40 & ast<45
replace anst_15kat= 6 if ast>=45 & ast<50
replace anst_15kat= 7 if ast>=50 & ast<55
replace anst_15kat= 8 if ast>=55 & ast<60
replace anst_15kat= 9 if ast>=60 & ast<65
replace anst_15kat= 10 if ast>= 65 & ast<70
replace anst_15kat= 11 if ast>= 70 & ast<75
replace anst_15kat= 12 if ast>=75 & ast<80
replace anst_15kat= 13 if ast>=80 & ast<85
replace anst_15kat= 14 if ast>=85 & ast<90
replace anst_15kat= 15 if ast>=90 & ast<96
replace anst_15kat= 12 if ast==99
gen alder_5cat=int(age/5) 

egen age_sex_ed_ast=group(kon alder_5cat anst_15kat ter_ed)
gen ln_inc = ln(p_ink)
reghdfe  ln_inc, abs(age_sex_ed_ast mun) res(inc_res)
egen std_inc_res=std(inc_res)
duplicates drop
keep if std_inc_res!=.
gen year = `year'
keep p_id sni69 year std_inc_res
compress 
append using ink_residuals.dta
save ink_residuals.dta, replace 
}

*** 1979-1989***

forvalues  year =1979/1989 {
*** utb ***
use "edu_panel70_90.dta"
keep if year ==`year'
gen ter_ed=ed_level>=5 if ed_level!=.
keep p_id ter_ed 
save temp, replace 
*** ink, sni, kommun ***
odbc load, exec("select p_id=LopNr, p_ink=CARB, mun=KOMB, sni69=SNI from IoT_`year'") dsn("P1129") clear	

joinby p_id using temp, unmatched(both)
drop _merge
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta", unmatched(master)
ren Kon kon
ren FodelseAr yob
destring yob, replace 
gen age = `year'-yob
keep p_id ter_ed p_ink mun sni69 kon age 
destring *, replace 	

joinby sni69 using sni69_92, unmatched(master)

gen ast1 = int(sni92/1000) 
duplicates drop
drop if p_id ==.
bysort p_id: egen ast= mode(ast1), minmode
keep p_id mun p_ink ter_ed age kon ast sni69

****gör om så det stämmer för SNI69 och för tvåsiffrig kod
gen     anst_15kat=.
replace anst_15kat= 1 if (ast>=1 & ast<5) 
replace anst_15kat= 2 if ast>=5 & ast<10
replace anst_15kat= 3 if ast>=10 & ast<15
replace anst_15kat= 4 if ast>=15 & ast<40
replace anst_15kat= 5 if ast>=40 & ast<45
replace anst_15kat= 6 if ast>=45 & ast<50
replace anst_15kat= 7 if ast>=50 & ast<55
replace anst_15kat= 8 if ast>=55 & ast<60
replace anst_15kat= 9 if ast>=60 & ast<65
replace anst_15kat= 10 if ast>= 65 & ast<70
replace anst_15kat= 11 if ast>= 70 & ast<75
replace anst_15kat= 12 if ast>=75 & ast<80
replace anst_15kat= 13 if ast>=80 & ast<85
replace anst_15kat= 14 if ast>=85 & ast<90
replace anst_15kat= 15 if ast>=90 & ast<96
replace anst_15kat= 12 if ast==99
gen alder_5cat=int(age/5) 

egen age_sex_ed_ast=group(kon alder_5cat anst_15kat ter_ed)
gen ln_inc = ln(p_ink)
reghdfe  ln_inc, abs(age_sex_ed_ast mun) res(inc_res)
egen std_inc_res=std(inc_res)
duplicates drop
keep if std_inc_res!=.
gen year = `year'
keep p_id sni69 year std_inc_res
compress 
append using ink_residuals.dta
duplicates drop
save ink_residuals.dta, replace 
}

*** 1990-2018 **

forvalues year =1990/2018 {
*** utb, sni, ålder, ink, syss ***
odbc load, exec("select * from LISA_`year'") dsn("P1129") clear	
if `year' >=2011 {
keep LopNr AstSNI2007 FodelseAr InkFNetto LoneInk SyssStat11 Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI2007 sni07
ren FodelseAr yob 
ren InkFNetto f_ink
ren LoneInk l_ink 
ren SyssStat11 syss
destring sni07 yob syss, replace
keep p_id sni07 yob f_ink l_ink syss ter_ed
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni07 age p_ink syss ter_ed 
*** lägg på sni 02 ***
gen astsni2007 = sni07
joinby astsni2007 using key_sni07_sni02, unmatched(master)
gen sni02 = modsni2002
keep p_id sni02 age p_ink syss ter_ed 
duplicates drop
save temp, replace	
}
if `year' <=2010 & `year' >=2004 {
keep LopNr AstSNI2002 FodelseAr InkFNetto LoneInk SyssStatJ	Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI2002 sni02
ren FodelseAr yob 
ren InkFNetto f_ink
ren LoneInk l_ink 
ren SyssStatJ syss
destring syss yob sni02, replace
keep p_id sni02 yob f_ink l_ink syss ter_ed 
destring *, replace 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni02 age p_ink syss ter_ed 
save temp, replace	
}
if `year' ==2003 {
keep LopNr AstSNI2002 FodelseAr Fink LoneInk SyssStat Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI2002 sni02
ren FodelseAr yob 
ren Fink f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring sni02 yob syss, replace
keep p_id sni02 yob f_ink l_ink syss ter_ed 
destring *, replace 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni02 age p_ink syss ter_ed 
save temp, replace		
}
if `year' ==2002 {
keep LopNr AstSNI2002 FodelseAr FInk LoneInk SyssStat Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI2002 sni02
ren FodelseAr yob 
ren FInk f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring sni02 yob syss, replace
keep p_id sni02 yob f_ink l_ink syss ter_ed 
destring *, replace 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni02 age p_ink syss ter_ed 
save temp, replace	
}	
if `year' <=2001 & `year' >=1998 {
keep LopNr AstSNI92 FodelseAr Fink LoneInk SyssStat Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI92 sni92
ren FodelseAr yob 
ren Fink f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring syss sni92 yob, replace
keep p_id sni92 yob f_ink l_ink syss ter_ed 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni92 age p_ink syss ter_ed 
save temp, replace	
}
if `year' ==1997 {
keep LopNr AstSNI92 FodelseAr FInk LoneInk SyssStat Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI92 sni92
ren FodelseAr yob 
ren FInk f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring syss sni92 yob, replace
keep p_id sni92 yob f_ink l_ink syss ter_ed 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni92 age p_ink syss ter_ed 
save temp, replace	
}
if `year' <=1996 & `year' >=1993 {
keep LopNr AstSNI92 FodelseAr Fink LoneInk SyssStat Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI92 sni92
ren FodelseAr yob 
ren Fink f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring syss sni92 yob, replace
keep p_id sni92 yob f_ink l_ink syss ter_ed 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni92 age p_ink syss ter_ed 
save temp, replace	
}
if `year' <=1992 & `year' >=1991 {
keep LopNr AstSNI92 FodelseAr Fink LoneInk SyssStatG Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
	gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI92 sni92
ren FodelseAr yob 
ren Fink f_ink
ren LoneInk l_ink 
ren SyssStat syss
destring sni92 yob syss, replace
keep p_id sni92 yob f_ink l_ink syss ter_ed 
destring syss yob sni92, replace 
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni92 age p_ink syss ter_ed 
save temp, replace	
}
if `year' ==1990 {
keep LopNr AstSNI92 FodelseAr FInk90 LoneInk SyssStatG Sun2000niva
ren Sun2000niva Sun2000Niva
replace Sun2000Niva="" if Sun2000Niva=="*" | Sun2000Niva=="-"
	destring Sun2000Niva, replace force
	replace  Sun2000Niva=. if Sun2000Niva>=999
	gen ed_level=.
	replace ed_lev= 1 if Sun2000Niva>=100 & Sun2000Niva<200
	replace ed_lev= 2 if Sun2000Niva>=200 & Sun2000Niva<300
	replace ed_lev= 3 if Sun2000Niva>=300 & Sun2000Niva<330
	replace ed_lev= 4 if Sun2000Niva>=330 & Sun2000Niva<400
	replace ed_lev= 5 if Sun2000Niva>=410 & Sun2000Niva<530
	replace ed_lev= 6 if Sun2000Niva>=530 & Sun2000Niva<600
	replace ed_lev= 7 if Sun2000Niva>=600 & Sun2000Niva<=999
gen ter_ed=ed_level>=5 if ed_level!=.	
ren LopNr p_id 	
ren AstSNI92 sni92
ren FodelseAr yob 
ren FInk90 f_ink
ren LoneInk l_ink 
ren SyssStatG syss
destring sni92 yob syss, replace
gen age = `year'-yob
drop if age<18 | age>=65
gen p_ink =l_ink +f_ink  
replace p_ink =l_ink if f_ink<0
keep p_id sni92 age p_ink syss ter_ed 
save temp, replace	
}
*** kommun ***
if `year' >=1997 {
odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_ValDist_`year'") dsn("P1129") clear
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace 
}
if `year' <=1996 {
odbc load, exec("select p_id=LopNr,  mun=kommun from GeoData_SAMS_`year'") dsn("P1129") clear
joinby p_id using temp, unmatched(both)
drop _merge
save temp, replace 
}
*** kön ***
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta"
gen year=`year'
ren Kon kon
*** ink som missing om sysselsättning är annat än jobb **
replace p_ink=. if syss!=1
if `year' >= 2002{
	gen ast = sni02
}
if `year' <=2001 {
	gen ast = sni92
}
keep p_id mun p_ink ter_ed age kon ast year sni*
duplicates drop
** sektor ***
gen     anst_15kat=.
replace anst_15kat= 1 if (ast>=1000 & ast<5000) 
replace anst_15kat= 2 if ast>=5000 & ast<10000
replace anst_15kat= 3 if ast>=10000 & ast<15000
replace anst_15kat= 4 if ast>=15000 & ast<40000
replace anst_15kat= 5 if ast>=40000 & ast<45000
replace anst_15kat= 6 if ast>=45000 & ast<50000
replace anst_15kat= 7 if ast>=50000 & ast<55000
replace anst_15kat= 8 if ast>=55000 & ast<60000
replace anst_15kat= 9 if ast>=60000 & ast<65000
replace anst_15kat= 10 if ast>= 65000 & ast<70000
replace anst_15kat= 11 if ast>= 70000 & ast<75000
replace anst_15kat= 12 if ast>=75000 & ast<80000
replace anst_15kat= 13 if ast>=80000 & ast<85000
replace anst_15kat= 14 if ast>=85000 & ast<90000
replace anst_15kat= 15 if ast>=90000 & ast<96000
replace anst_15kat= 12 if ast==99000

gen alder_5cat=int(age/5) 

egen age_sex_ed_ast=group(kon alder_5cat anst_15kat ter_ed)
gen ln_inc = ln(p_ink)
reghdfe  ln_inc, abs(age_sex_ed_ast mun) res(inc_res)
egen std_inc_res=std(inc_res)
destring *, replace 
keep p_id sni* year std_inc_res
compress 
append using ink_residuals.dta
save ink_residuals.dta, replace 
}
}
}


use pol_std_inc_res.dta, clear
gen year = valar 
keep p_id year pol 
keep if pol==1
collapse(min)year, by(p_id)
ren year pol_first
save pol_first, replace
use ink_residuals.dta, clear
keep p_id year std_inc_res
keep if std!=.

joinby p_id using pol_first, unmatched(master)
drop if year >= pol_first & pol_first!=.
collapse (mean)std_inc_res, by(p_id)
egen std_inc_res_b=std(std_inc_res)
drop std_inc_res
ren std_inc_res_b std_inc_res
compress 
save "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\inc_res.dta", replace
